<?php

namespace App\Model\Auth;


use App\Model\Model;
use function Hyperf\Config\config;

/**
 * 菜单表
 */
class Role extends Model
{
    protected ?string $table = 'auth_role';
    // 允许批量赋值的字段
    protected array $fillable = [
        'name',
        'remarks',
        'status',
        'rules'
    ];

    // 字段类型转换
    protected array $casts = [
        'id' => 'integer',
        'status' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'rules' => 'string',
        'name' => 'string'
    ];

    public function setRulesAttribute($value): string
    {
        if ((!is_array($value) && $value == "*") || !$value) return $value;
        return $this->attributes['rules'] = implode(",", $value);
    }

    public function getRulesAttribute($value): array|string| null
    {
        if ($value == "*" || !$value) return $value;
        return array_map('intval', explode(",", $value));
    }


}
